package cn.jsu.oj.dispatchServer.satellite.util;


import cn.jsu.oj.dispatchServer.satellite.pojo.net.LogPointVO;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 该工具仅用于切分日志，并不可通用
 * @Author : LuoLin
 */
public class LogPointUtil {


    public static List<LogPointVO> Point(BufferedReader reader) throws IOException {
        ArrayList<LogPointVO> logPointVOS = new ArrayList<>();
        String str = "";
        String flag;
        int i =0;
        while ( (str=reader.readLine()) != null) {
            if (str.length() <=10) {
                continue;
            }
            flag = str.substring(0,10);
            if (cn.jsu.oj.dispatchServer.satellite.util.RegexUtil.find(flag,"^\\d{1,4}-(?:1[0-2]|0?[1-9])-(?:0?[1-9]|[1-2]\\d|30|31)$")) {
                LogPointVO logPointVO = new LogPointVO(i, str.substring(0, 29));
                logPointVOS.add(logPointVO);
                i++;
            }
        }
        return logPointVOS;
    }

    public static BufferedReader getBuffer(byte[] bytes) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        InputStreamReader streamReader = new InputStreamReader(byteArrayInputStream);
        return new BufferedReader(streamReader);
    }


    public static String getLogDetails(Integer index,byte[] bytes) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
//        byte[] bytes = new byte[0];
//        try(FileInputStream fileInputStream = new FileInputStream("src/main/java/cn/jsu/oj/error.2022-07-08.0.log")) {
//            //将文件流读取为字节数组
//            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//            byte[] buffers = new byte[1024];
//            int length;
//            while ((length = fileInputStream.read(buffers)) != -1) {
//                outputStream.write(buffers, 0, length);
//            }
//            bytes = outputStream.toByteArray();

            BufferedReader buffer = getBuffer(bytes);
            int i = 0;
            String logDetail;
            boolean isDetail = false;
            while ((logDetail = buffer.readLine()) != null) {
                //校验日志开头格式如下： 2022-10-10
                if (RegexUtil.find(logDetail.substring(0,10),"^\\d{1,4}-(?:1[0-2]|0?[1-9])-(?:0?[1-9]|[1-2]\\d|30|31)$")) {
                    if (isDetail) {
                        break;
                    }//
                    if (i  != index) {
                        i++;
                    }
                    else {
                        isDetail = true;
                        stringBuilder.append(logDetail);
                    }
                }
                else {
                    if (isDetail) {
                        stringBuilder.append("\n")
                                .append(logDetail);
                    }
                    //if no start count LogDetail ,Nothing happen
                }
            }

        return stringBuilder.toString();
    }

}
